package edu.gatech.stolen.tea;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class CreateItemActivity extends Activity {
	
	public static final String TAG = "Create Item Page";
	
	private EditText name;
	private EditText description;
	private EditText location;
	private Spinner[] spinners = new Spinner[7];
	private Month[] months = new Month[12];
	private Day[] days = new Day[31];
	private Integer[] years = new Integer[50];
	private ItemType[] types = new ItemType[3];
	//private Month[] months2 = new Month[12];
	//private Day[] days2 = new Day[31];
	//private Integer[] years2 = new Integer[50];
	private Button creButton;
	private Button canButton;
	private TextView createInfo;
	
	
    /** Called when the activity is first created.
     * initialize the buttons and text fields
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createitem);
        
        populateData();
        
        name = (EditText) findViewById(R.id.eNameEditText_createitem);
        description = (EditText) findViewById(R.id.eDescriptionEditText_createitem);
        location = (EditText) findViewById(R.id.eLocationEditText_createitem);
        
        spinners[0] = (Spinner) findViewById(R.id.sMonthGivenSpinner_createitem);
        ArrayAdapter<Month> mGivenAdapter = new ArrayAdapter<Month>(this, R.layout.monthgiven, R.id.tMonthGivenTextView_monthgiven, months);
        spinners[0].setAdapter(mGivenAdapter);
        spinners[0].setSelection(0);
        
        spinners[1] = (Spinner) findViewById(R.id.sDayGivenSpinner_createitem);
        ArrayAdapter<Day> dGivenAdapter = new ArrayAdapter<Day>(this, R.layout.daygiven, R.id.tDayGivenTextView_daygiven, days);
        spinners[1].setAdapter(dGivenAdapter);
        spinners[1].setSelection(0);
        
        spinners[2] = (Spinner) findViewById(R.id.sYearGivenSpinner_createitem);
        ArrayAdapter<Integer> yGivenAdapter = new ArrayAdapter<Integer>(this, R.layout.yeargiven, R.id.tYearGivenTextView_yeargiven, years);
        spinners[2].setAdapter(yGivenAdapter);
        spinners[2].setSelection(0);
        
        spinners[3] = (Spinner) findViewById(R.id.sMonthDueSpinner_createitem);
        ArrayAdapter<Month> mDueAdapter = new ArrayAdapter<Month>(this, R.layout.monthdue, R.id.tMonthDueTextView_monthdue, months);
        spinners[3].setAdapter(mDueAdapter);
        spinners[3].setSelection(0);
        
        spinners[4] = (Spinner) findViewById(R.id.sDayDueSpinner_createitem);
        ArrayAdapter<Day> dDueAdapter = new ArrayAdapter<Day>(this, R.layout.daydue, R.id.tDayDueTextView_daydue, days);
        spinners[4].setAdapter(dDueAdapter);
        spinners[4].setSelection(0);
        
        spinners[5] = (Spinner) findViewById(R.id.sYearDueSpinner_createitem);
        ArrayAdapter<Integer> yDueAdapter = new ArrayAdapter<Integer>(this, R.layout.yeardue, R.id.tYearDueTextView_yeardue, years);
        spinners[5].setAdapter(yDueAdapter);
        spinners[5].setSelection(0);
        
        spinners[6] = (Spinner) findViewById(R.id.sItemTypeSpinner_createitem);
        ArrayAdapter<ItemType> iTypeAdapter = new ArrayAdapter<ItemType>(this, R.layout.itemtype, R.id.tItemTypeTextView_itemtype, types);
        spinners[6].setAdapter(iTypeAdapter);
        spinners[6].setSelection(0);
        
        creButton = (Button) findViewById(R.id.bCreateButton_createitem);
        canButton = (Button) findViewById(R.id.bCancelButton_createitem);
        createInfo = (TextView) findViewById(R.id.lblRegisterInfo);
        

        /**
         * give register button a click listener
         */
        creButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				onCreButtonClicked();
				
			}
        });
        
        /**
         * give cancel button a register listener
         */
        canButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				onCanButtonClicked();
				
			}
        });
        
    	
    }
    /**
     * if register fails, notify the user
     * if register successful, go to registered page
     */
    private void onCreButtonClicked() {
    	
    	Log.v(TAG, "Create Button Clicked");
    	createNewItem();
    	
    }
    
    /**
     * if cancel button clicked, go back to register page
     */
    private void onCanButtonClicked() {
    	
    	Log.v(TAG, "Cancel Button Clicked");
    	goToItemDisplayListPage();
    	
    }
    
    /**
     * register a new user in accounts
     * @return true if registered successfully, false if otherwise
     */
    private void createNewItem() {
    	
    	String na = name.getText().toString();
    	String des = description.getText().toString();
    	String loc = location.getText().toString();
    	
    	Month mG = months[spinners[0].getSelectedItemPosition()];
    	Day day = days[spinners[1].getSelectedItemPosition()];
    	int year = years[spinners[2].getSelectedItemPosition()];
		mG.setDay(day); mG.setYear(year);
		
		Month mD = months[spinners[3].getSelectedItemPosition()];
		day = days[spinners[4].getSelectedItemPosition()];
    	year = years[spinners[5].getSelectedItemPosition()];
    	mD.setDay(day); mD.setYear(year);
    	
    	ItemType type = types[spinners[6].getSelectedItemPosition()];
		
    	if(ItemManager.createItem(na, des, loc, mG, mD, type)){
    		goToItemDisplayListPage();
    		finish();
    	} else {
    		createInfo.setText("Creation failed. Item already exists.");
    	}
    	
    }
    
    private void populateData() {
    	
        months[0] = Month.JANUARY; months[1] = Month.FEBRUARY; months[2] = Month.MARCH; months[3] = Month.APRIL;
        months[4] = Month.MAY; months[5] = Month.JUNE; months[6] = Month.JULY; months[7] = Month.AUGUST;
        months[8] = Month.SEPTEMBER; months[9] = Month.OCTOBER; months[10] = Month.NOVEMBER; months[11] = Month.DECEMBER;
        
        days[0] = Day.FIRST; days[1] = Day.SECOND; days[2] = Day.THIRD; days[3] = Day.FOURTH; days[4] = Day.FIFTH;
        days[5] = Day.SIXTH; days[6] = Day.SEVENTH; days[7] = Day.EIGHTH; days[8] = Day.NINTH; days[9] = Day.TENTH;
        days[10] = Day.ELEVENTH; days[11] = Day.TWELVTH; days[12] = Day.THIRTHEENTH; days[13] = Day.FOURTEENTH; days[14] = Day.FIFTEENTH;
        days[15] = Day.SIXTENTH; days[16] = Day.SEVENTEENTH; days[17] = Day.EIGTEENTH; days[18] = Day.NINETEENTH; days[19] = Day.TWENTIETH;
        days[20] = Day.TWENTY_FIRST; days[21] = Day.TWENTY_SECOND; days[22] = Day.TWENTY_THIRD; days[23] = Day.TWENTY_FOURTH; days[24] = Day.TWENTY_FIFTH;
        days[25] = Day.TWENTY_SIXTH; days[26] = Day.TWENTY_SEVETH; days[27] = Day.TWENTY_EIGHTH; days[28] = Day.TWENTY_NINTH; days[29] = Day.THIRTIETH;
        days[30] = Day.THIRTY_FIRST;
        
        for(int i = 0; i < 50; i++){
        	years[i] = Integer.valueOf(i + 2012);
        }
        
        types[0] = ItemType.PERSONAL; types[1] = ItemType.SCHOOL; types[2] = ItemType.WORK;
    	
    }
    
    /**
     * go to register successful page
     */
    protected void goToItemDisplayListPage() {
    	
    	Intent i = new Intent(this, ItemDisplayListActivity.class);
    	startActivity(i);
   	
    } 

}